package com.yonyou.brigade.third_parties.ystrip.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.ystrip.model.YStripSyncOrderParam;
import com.yonyou.brigade.third_parties.ystrip.model.YStripUser;
import com.yonyou.brigade.third_parties.ystrip.service.IYStripPublicService;
import com.yonyou.brigade.third_parties.ystrip.service.IYStripUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/testYStrip")
@RestController
public class TestYStripController {

    @Qualifier("YStripPublicServiceImpl")
    @Autowired
    IYStripPublicService iyStripPublicService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("YStripUserServiceImpl")
    private IYStripUserService iyStripUserService;

    @RequestMapping(value = "/createUser",method = RequestMethod.POST)
    public JSONObject createUser(@RequestBody List<YStripUser> yStripUsers){
        String respStr = iyStripPublicService.insertUser(yStripUsers);
        JSONObject resultJSON = new JSONObject();
        resultJSON.put("data", JSON.parseArray(respStr));
        return resultJSON;
    }

    @RequestMapping(value = "/syncAllUser",method = RequestMethod.GET)
    public JSONObject syncAllUser2YStrip(){
        JSONObject jsonObject = new JSONObject();
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel("ystrip");
        if(briChannels != null && briChannels.size() > 0){
            briChannels.forEach(briChannel -> {
                if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    iyStripUserService.syncAllUserToYStrip(tenant,orgpk);
                }
            });
        }
        jsonObject.put("code","0");
        jsonObject.put("information","同步成功");
        return jsonObject;
    }

    @RequestMapping(value = "/ssoLogin/{jumpType}",method = RequestMethod.POST)
    public JSONObject ssoLogin(@RequestBody YStripUser yStripUser,@PathVariable String jumpType){
        String url = iyStripPublicService.ssoLogin(jumpType,yStripUser);
        JSONObject resultJSON = new JSONObject();
        resultJSON.put("code","0");
        resultJSON.put("url",url);
        return resultJSON;
    }
    @RequestMapping(value = "/syncOrder",method = RequestMethod.POST)
    public JSONObject testSyncOrder(@RequestBody YStripSyncOrderParam yStripSyncOrderParam){
        iyStripPublicService.syncOrders(yStripSyncOrderParam,5,yStripSyncOrderParam.getTenant());
        JSONObject resultJSON = new JSONObject();
        resultJSON.put("code","0");
        resultJSON.put("information","同步成功");
        return resultJSON;
    }
}
